package com.ruoyi.web.controller.proappli;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.domain.ProApplicationPayment;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.service.IProApplicationPaymentService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/ruoyi/paymentVerify")
public class ProApplicationVerifyController extends BaseController {
    private String prefix = "ruoyi/payment";

    @Autowired
    private IProApplicationPaymentService proApplicationPaymentService;

    @RequiresPermissions("ruoyi:paymentVerify:view")
    @GetMapping()
    public String payment() {
        return prefix + "/paymentVerify";
    }


    /**
     * 详情页面传递参数
     *
     * @param bId
     * @param map
     * @return
     */
    @RequiresPermissions("ruoyi:paymentVerify:view")
    @GetMapping("/payments/{bId}")
    public String logs(@PathVariable("bId") Long bId, ModelMap map) {
        ProApplicationPayment proApplicationPayment = proApplicationPaymentService.selectProApplicationPaymentById(bId);
        map.put("proApplicationPayment", proApplicationPayment);
        return prefix + "/payments";
    }

    /**
     * 查询付款申请单列表
     */
    @RequiresPermissions("ruoyi:paymentVerify:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ProApplicationPayment proApplicationPayment) {
        startPage();
        List<ProApplicationPayment> list = proApplicationPaymentService.selectProApplicationPaymentList(proApplicationPayment);
        return getDataTable(list);
    }

    /**
     * 查询财务能看得到的审批
     */
    @RequiresPermissions("ruoyi:contract:listss")
    @PostMapping("/listss")
    @ResponseBody
    public TableDataInfo listss(Long sId) {
        startPage();
        //查询所有审批,使用流处理过滤出状态为1的审批,也就是财务可以看到的审批
        List<ProApplicationPayment> list = proApplicationPaymentService.selectProjectContractlists(sId).stream().filter(p -> p.getaStatus()==1).collect(Collectors.toList());
        return getDataTable(list);
    }

    /**
     * 查询对应项目下的对应的付款申请单
     */
    @RequiresPermissions("ruoyi:contract:lists")
    @PostMapping("/lists")
    @ResponseBody
    public TableDataInfo lists(Long idss) {
        startPage();
        List<ProApplicationPayment> list = proApplicationPaymentService.selectProjectContractlists(idss);
        return getDataTable(list);
    }

    /**
     * 导出付款申请单列表
     */
    @RequiresPermissions("ruoyi:paymentVerify:export")
    @Log(title = "付款申请单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ProApplicationPayment proApplicationPayment) {
        List<ProApplicationPayment> list = proApplicationPaymentService.selectProApplicationPaymentList(proApplicationPayment);
        ExcelUtil<ProApplicationPayment> util = new ExcelUtil<ProApplicationPayment>(ProApplicationPayment.class);
        return util.exportExcel(list, "payment");
    }

    /**
     * 新增付款申请单
     */
    @GetMapping("/add/{pId}")
    public String add(@PathVariable("pId") Long pId, ModelMap map) {
        map.put("pId", pId);
        return prefix + "/add";
    }

    /**
     * 新增保存付款申请单
     */
    @RequiresPermissions("ruoyi:paymentVerify:add")
    @Log(title = "付款申请单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ProApplicationPayment proApplicationPayment) {
        String name = ShiroUtils.getLoginName();
        proApplicationPayment.setaProposer(name);
        return toAjax(proApplicationPaymentService.insertProApplicationPayment(proApplicationPayment));
    }

    /**
     * 驳回请假需求
     */
    @GetMapping("/edit/{aId}")
    public String edit(@PathVariable("aId") Long aId, ModelMap mmap) {
        proApplicationPaymentService.editparoStatus(aId);
        ProApplicationPayment proApplicationPayment = proApplicationPaymentService.selectProApplicationPaymentById(aId);
        mmap.put("proApplicationPayment", proApplicationPayment);
        return prefix + "/paymentVerifyedit";
    }

    /**
     * 修改保存付款申请单
     */
    @RequiresPermissions("ruoyi:paymentVerify:edit")
    @Log(title = "付款申请单", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ProApplicationPayment proApplicationPayment) {
        return toAjax(proApplicationPaymentService.updateProApplicationPayment(proApplicationPayment));
    }

    /**
     * 删除付款申请单
     */
    @RequiresPermissions("ruoyi:paymentVerify:remove")
    @Log(title = "付款申请单", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(proApplicationPaymentService.deleteProApplicationPaymentByIds(ids));
    }

    /**
     * 提交申请
     */
    @Log(title = "付款申请业务", businessType = BusinessType.UPDATE)
    @PostMapping("/submitApply")
    @ResponseBody
    public AjaxResult submitApply(Long id) {
        proApplicationPaymentService.finExaStatus(id);
        return success();
    }

    /**
     * 财务通过申请
     */
    @Log(title = "请假业务", businessType = BusinessType.UPDATE)
    @PostMapping( "/consent")
    @ResponseBody
    public AjaxResult consent(Long id) {
        proApplicationPaymentService.finExaStatuss(id);
        return success();
    }
}
